package com.libre.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.libre.auth.entity.LoginLog;
import com.libre.auth.mapper.LoginLogMapper;
import com.libre.auth.service.LoginLogService;
import com.libre.common.utils.DateTimeUtil;
import com.libre.common.utils.ip.CZIPAddressUtils;
import com.libre.common.utils.ip.IpUtil;
import lombok.extern.slf4j.Slf4j;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {

    @Override
    public IPage<LoginLog> findPage(Page page, LoginLog loginLog) {
        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("login_time");
        Page pages = this.page(page, queryWrapper);
        return pages;
    }

    @Override
    public Map<String, Long> statistics() {
        QueryWrapper<LoginLog> totalQueryWrapper = new QueryWrapper<>();
        totalQueryWrapper.eq("is_delete", "0");
        long total = this.count(totalQueryWrapper);
        QueryWrapper<LoginLog> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.eq("DATE_FORMAT(login_time,'%Y-%m-%d')", DateTimeUtil.format(new Date(), DateTimeUtil.DATE));
        dayQueryWrapper.eq("is_delete", "0");
        long today = this.count(dayQueryWrapper);
        Map<String, Long> map = Maps.newHashMap();
        map.put("total", total);
        map.put("today", today);
        return map;
    }


    @Override
    public void saveLoginLog(HttpServletRequest request,String username) {
        /******************** 获取IP以及区域 *******************/
        String ipAddress = IpUtil.getIpAddr(request);
        log.error("IP ======================================== " + ipAddress);
        CZIPAddressUtils ip = new CZIPAddressUtils();
        ip.init();
        System.out.println("IP地址[" + ipAddress + "]获取到的区域信息:" + ip.getIPLocation(ipAddress).getCountry() + ", 获取到的城市:" + ip.getIPLocation(ipAddress).getCity() + ", 运营商:" + ip.getIPLocation(ipAddress).getArea());

        /******************** 获取浏览器信息 *******************/
        //获取浏览器信息
        String ua = request.getHeader("User-Agent");
        //转成UserAgent对象
        UserAgent userAgent = UserAgent.parseUserAgentString(ua);
        //获取浏览器信息
        Browser browser = userAgent.getBrowser();
        //获取系统信息
        OperatingSystem os = userAgent.getOperatingSystem();
        //系统名称
        String system = os.getName();
        //浏览器名称
        String browserName = browser.getName();
        log.info("系统名称:"+system);
        log.info("浏览器名称:"+browserName);
        LoginLog loginLog = new LoginLog();
        loginLog.setUsername(username);
        loginLog.setIp(ipAddress);
        loginLog.setArea(ip.getIPLocation(ipAddress).getCountry());
        loginLog.setCity(ip.getIPLocation(ipAddress).getCity());
        loginLog.setOperator(ip.getIPLocation(ipAddress).getArea());
        loginLog.setSystem(system);
        loginLog.setBrowser(browserName);
        loginLog.setLoginTime(new Date());
        this.save(loginLog);
    }


}
